package com.hxkcloud.patient.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.adapter.AssessScoresAdapter;
import com.hxkcloud.patient.beans.AssessScoreBean;
import com.hxkcloud.patient.beans.AssessScoreWholeBean;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by william_sun on 2016-10-12.
 * Description:专科评估（调查问卷）得分列表
 */
public class AssessScoresActivity extends BaseActivity implements View.OnClickListener, PullToRefreshLayout.OnRefreshListener {

    private String patientId;
    private String assessId;
    private String title;
    private String name;

    //title
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private TextView tv_name;

    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recyclerView;

    private List<AssessScoreBean> datas;
    private AssessScoresAdapter adapter;
    private int pageIndex = 1;
    private int pageSize = 15;
    private int totalCount = 0;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_ASSESSSCORELIST_SUCCESS:
                AssessScoreWholeBean wholeBean = (AssessScoreWholeBean) msg.obj;
                if (!TextUtils.isEmpty(wholeBean.getTotalCount()))
                    totalCount = Integer.valueOf(wholeBean.getTotalCount());
                if (pageIndex == 1) {
                    datas.clear();
                    refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                } else {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                datas.addAll(wholeBean.getAssessList());
                adapter.setData(datas);
                break;
            case MessageCode.NETWORKCODE_ASSESSSCORELIST_FAILURE:
                if (msg.obj != null && !TextUtils.isEmpty((String) msg.obj)) {
                    toastMsg((String) msg.obj);
                    return;
                } else {
                    toastMsg(getResources().getString(R.string.datagetfail));
                }
                if (pageIndex == 1) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                } else {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assessscores);

        Intent iGet = getIntent();
        patientId = iGet.getStringExtra("patientid");
        assessId = iGet.getStringExtra("assessid");
        title = iGet.getStringExtra("title");
        name = iGet.getStringExtra("name");

        initView();
        initData();
    }


    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(title);
        iv_back.setOnClickListener(this);

        tv_name = (TextView) findViewById(R.id.tv_assessscores_name);
        tv_name.setText(name);

        refreshLayout = (PullToRefreshLayout) findViewById(R.id.refreshlayout_assessscores);
        recyclerView = (PullableRecyclerView) findViewById(R.id.recyclerview_assessscores);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(mLayoutManager);
        refreshLayout.setOnRefreshListener(this);

        datas = new ArrayList<>();
        adapter = new AssessScoresAdapter(mContext, datas, new AssessScoresAdapter.onAssessScoreSelectListener() {
            @Override
            public void onSelect(int position) {
                Intent intent = new Intent(mContext,
                        AssessFeedbackActivity.class);
                intent.putExtra("assessId", assessId);
                intent.putExtra("patientId", patientId);
                intent.putExtra("questionUserId", datas.get(position).getAnswerId());
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        loading();
        pageIndex = 1;
        YiJiaHuNetWorkUtils.assessScoreList(patientId, assessId, pageIndex, pageSize, handler);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
        }
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        initData();
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        if (datas.size() >= totalCount) {
            toastMsg(R.string.nomore);
        } else {
            loading();
            pageIndex++;
            YiJiaHuNetWorkUtils.assessScoreList(patientId, assessId, pageIndex, pageSize, handler);
        }
    }
}
